package com.ruoyi.community.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.community.mapper.SimpleNoticeContentMapper;
import com.ruoyi.community.domain.SimpleNoticeContent;
import com.ruoyi.community.service.ISimpleNoticeContentService;

/**
 * 公告Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-12-10
 */
@Service
public class SimpleNoticeContentServiceImpl implements ISimpleNoticeContentService 
{
    @Autowired
    private SimpleNoticeContentMapper simpleNoticeContentMapper;

    /**
     * 查询公告
     * 
     * @param noticeId 公告ID
     * @return 公告
     */
    @Override
    public SimpleNoticeContent selectSimpleNoticeContentById(String noticeId)
    {
        SimpleNoticeContent resultNotice = simpleNoticeContentMapper.selectSimpleNoticeContentById(noticeId);
        Long viewNumber = resultNotice.getViewNumber() + 1;
        SimpleNoticeContent updateNotice = new SimpleNoticeContent();
        updateNotice.setNoticeId(noticeId);
        updateNotice.setCommunityId(resultNotice.getCommunityId());
        updateNotice.setViewNumber(viewNumber);
        updateSimpleNoticeContent(updateNotice);
        return resultNotice;
//        return simpleNoticeContentMapper.selectSimpleNoticeContentById(noticeId);
    }

    /**
     * 查询公告列表
     * 
     * @param simpleNoticeContent 公告列表
     * @return 公告列表
     */
    @Override
    public List<SimpleNoticeContent> selectSimpleNoticeContentList(SimpleNoticeContent simpleNoticeContent)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        simpleNoticeContent.setCommunityId(Long.valueOf(loginUser.getCommunityId()));
        return simpleNoticeContentMapper.selectSimpleNoticeContentList(simpleNoticeContent);
    }

    /**
     * 新增公告
     * 
     * @param simpleNoticeContent 公告
     * @return 结果
     */
    @Override
    public int insertSimpleNoticeContent(SimpleNoticeContent simpleNoticeContent)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        simpleNoticeContent.setAuthor(loginUser.getUsername());
        simpleNoticeContent.setCommunityId(Long.valueOf(loginUser.getCommunityId()));
        String content = simpleNoticeContent.getContent();
        int pic_start = content.indexOf("<img");
        if (pic_start == -1){
            simpleNoticeContent.setPicUrl(null);
        }else {
            int pic_end = content.indexOf("\">", pic_start);
            String pic_url = content.substring(pic_start+10, pic_end);
            simpleNoticeContent.setPicUrl(pic_url);
        }
        return simpleNoticeContentMapper.insertSimpleNoticeContent(simpleNoticeContent);
    }

    /**
     * 修改公告
     * 
     * @param simpleNoticeContent 公告
     * @return 结果
     */
    @Override
    public int updateSimpleNoticeContent(SimpleNoticeContent simpleNoticeContent)
    {
        return simpleNoticeContentMapper.updateSimpleNoticeContent(simpleNoticeContent);
    }

    /**
     * 批量删除公告
     * 
     * @param noticeIds 需要删除的公告ID
     * @return 结果
     */
    @Override
    public int deleteSimpleNoticeContentByIds(String[] noticeIds)
    {
        return simpleNoticeContentMapper.deleteSimpleNoticeContentByIds(noticeIds);
    }

    /**
     * 删除公告信息
     * 
     * @param noticeId 公告ID
     * @return 结果
     */
    @Override
    public int deleteSimpleNoticeContentById(String noticeId)
    {
        return simpleNoticeContentMapper.deleteSimpleNoticeContentById(noticeId);
    }
}
